//
//  RootViewController.m
//  CbyTools
//
//  Created by 崔宝印 on 13-10-23.
//  Copyright (c) 2013年 崔 宝印. All rights reserved.
//

#import "RootViewController.h"
#import "NSString+URLEncoding.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (IBAction)encodeURL:(id)sender
{
    NSString *url = @"http://www.baidu.com?rsv_spt=1&wd=有道";
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[url URLEncodedString]]];
    
    //Synchronous 同步get
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
    NSLog(@"%@",[url URLEncodedString]);
    
    //Asynchronous 异步get, use the urlconnection delegate
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [connection start];
    
//    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//    NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
//    NSLog(@"%@",[url URLEncodedString]);
}

- (IBAction)decodeURL:(id)sender
{
    NSString *url = @"http://www.baidu.com?param1=%E7%99%BE%E5%BA%A6&type=%E8%B0%B7%E6%AD%8C";
    NSLog(@"%@", [url URLDecodedString]);
}

@end
